package com.wan.controller;

import com.wan.pojo.Authority;
import com.wan.pojo.Menu;
import com.wan.pojo.Page;
import com.wan.pojo.Role;
import com.wan.service.AuthorityService;
import com.wan.service.MenuService;
import com.wan.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

//角色管理控制器
@Controller
@RequestMapping("/admin/role")
public class RoleController {
    @Autowired
    private RoleService roleService;
    @Autowired
    private MenuService menuService;
    @Autowired
    private AuthorityService authorityService;
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public ModelAndView list(ModelAndView model){
        model.setViewName("role/list");
        return model;
    }
//    获取角色列表
    //包含搜索框模糊查询功能
    @RequestMapping(value = "/list",method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> getRoleList(Page page,@RequestParam(name = "name",required = false,defaultValue = "") String name){
        Map<String, Object> map = new HashMap<String, Object>();
        Map<String, Object> queryMap = new HashMap<String, Object>();
        queryMap.put("offset",page.getOffset());
        queryMap.put("pageSize",page.getRows());
        queryMap.put("name",name);
        map.put("rows",roleService.getRoleList(queryMap));
        map.put("total",roleService.getTotal(queryMap));
        return map;
    }
    //添加角色
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    @ResponseBody
    public Map<String,String> add(Role role){
        Map<String, String> map = new HashMap<String, String>();
        if(role==null){
            map.put("type","error");
            map.put("msg","请填写正确的角色信息！");
            return map;
        }
        if(StringUtils.isEmpty(role.getName())){
            map.put("type","error");
            map.put("msg","请填写角色名称！");
            return map;
        }
        if(StringUtils.isEmpty(role.getRemark())){
            map.put("type","error");
            map.put("msg","请填写角色备注");
            return map;
        }
        if(roleService.add(role)<=0){
            map.put("type","error");
            map.put("msg","添加失败，请联系管理员！");
            return map;
        }
        map.put("type","success");
        map.put("msg","添加成功！");
        return map;

    }
    //修改角色
    @RequestMapping(value = "/edit",method=RequestMethod.POST)
    @ResponseBody
    public Map<String,String> edit(Role role){
        Map<String, String> map = new HashMap<String, String>();
        if(role==null){
            map.put("type","error");
            map.put("msg","请选择正确的角色信息！");
            return map;
        }
        if(StringUtils.isEmpty(role.getName())){
            map.put("type","error");
            map.put("msg","请填写角色名称！");
            return map;
        }
        if(StringUtils.isEmpty(role.getRemark())){
            map.put("type","error");
            map.put("msg","请填写角色备注");
            return map;
        }
        if(roleService.edit(role)<=0){
            map.put("type","error");
            map.put("msg","修改失败，请联系管理员！");
            return map;
        }
        map.put("type","success");
        map.put("msg","修改成功！");
        return map;
    }
    //删除角色
    @RequestMapping(value = "/delete",method = RequestMethod.POST)
    @ResponseBody
    public Map<String,String> delete(Long id){
        Map<String, String> map = new HashMap<String, String>();
        if(id==null){
            map.put("type","error");
            map.put("msg","请选择要删除的角色信息!");
            return map;
        }
        try {
            if(roleService.delete(id) <= 0){
                map.put("type", "error");
                map.put("msg", "删除失败，请联系管理员！");
                return map;
            }
        } catch (Exception e) {//authority表中添加了外键，menuId和menu表中的id对应，roleId和role表中的id对应
                               //如果角色下存在权限或者用户信息，数据库无法删除，会报异常
                               //这里捕捉异常，并提示错误
            // TODO: handle exception
            map.put("type", "error");
            map.put("msg", "该角色下存在权限或者用户信息，不能删除！");
            return map;
        }
        map.put("type","success");
        map.put("msg","删除成功！");
        return map;
    }
    //获取所有菜单信息
    @RequestMapping(value = "/getAllMenu",method = RequestMethod.POST)
    @ResponseBody
    public List<Menu> getAllMenu(){
        Map<String, Object> queryMap = new HashMap<String, Object>();
        queryMap.put("offset", 0);
        queryMap.put("pageSize", 99999);
        List<Menu> menuList = menuService.getMenuList(queryMap);
        return menuList;
    }

    //编辑权限
    @RequestMapping(value = "/addAuthority",method = RequestMethod.POST)
    @ResponseBody
    public Map<String,String> addAuthority(
            @RequestParam(name="ids",required=true) String ids,
            @RequestParam(name="roleId",required=true) Long roleId
    ){
        Map<String, String> map = new HashMap<String, String>();
        if(StringUtils.isEmpty(ids)){
            map.put("type", "error");
            map.put("msg", "请选择相应的权限！");
            return map;
        }
        if(roleId == null){
            map.put("type", "error");
            map.put("msg", "请选择相应的角色！");
            return map;
        }
        //substring() 方法用于提取字符串中介于两个指定下标之间的字符。
        if(ids.contains(",")){
            ids = ids.substring(0,ids.length()-1);//将最后一个逗号去掉
        }
        //split() 方法用于把一个字符串分割成字符串数组。
        String[] idArr = ids.split(",");
        if(idArr.length > 0){
            //将之前的权限删掉
            int i = authorityService.deleteByRoleId(roleId);
        }
        for(String id:idArr){
            Authority authority = new Authority();
            authority.setMenuId(Long.valueOf(id));
            authority.setRoleId(roleId);
            authorityService.add(authority);
        }
        map.put("type", "success");
        map.put("msg", "权限编辑成功！");
        return map;
    }
    //通过角色Id查询其所拥有的权限集合
    @RequestMapping(value = "/findListByRoleId",method = RequestMethod.POST)
    @ResponseBody
    public List<Authority> findListByRoleId(@RequestParam(name="roleId",required=true) Long roleId){
        return authorityService.findListByRoleId(roleId);
    }
}

